/*
 *所有关于Container类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*ContainerService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using MyMES.Infrastructure;
using MyMES.Dto;
using Azure.Core;
using MyMES.Repositories;

namespace MyMES.Services
{
    public partial class ContainerService
    {
        private readonly IBaseRepository<Container> _containerRepository;

        public ContainerService(IBaseRepository<Container> containerRepository)
        {
            _containerRepository = containerRepository;
        }

        /// <summary>
        /// 获取装箱管理信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<APIPage<Container>> GetContainerPageList(GetContainerPageListDTO param)
        {
            var list = _containerRepository.GetAll();

            if (!string.IsNullOrEmpty(param.PackagingCode))
            {
                list = list.Where(e => e.PackagingCode.Contains(param.PackagingCode));
            }

            if (!string.IsNullOrEmpty(param.SalesCode))
            {
                list = list.Where(e => e.SalesCode.Contains(param.SalesCode));
            }

            if (!string.IsNullOrEmpty(param.ClientName))
            {
                list = list.Where(e => e.ClientName.Contains(param.ClientName));
            }

            if (!string.IsNullOrEmpty(param.InspectorName))
            {
                list = list.Where(e => e.InspectorName.Contains(param.InspectorName));
            }

            var supplierpaging = new APIPage<Container>()
            {
                Data = list.Skip((param.PageIndex - 1) * param.PageSize).Take(param.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / param.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增装箱管理
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public async Task<int> CreateContainer(Container container)
        {
            var result = await _containerRepository.AddAsync(container);

            return result;
        }

        /// <summary>
        /// 删除装箱管理信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> DeleteContainer(Container param)
        {
            var result = await _containerRepository.Remove(param);

            return result;
        }

        /// <summary>
        /// 修改装箱管理信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> UpdateContainer(Container param)
        {
            var result = await _containerRepository.UpdateAsync(param);

            return result;
        }
    }
}
